package com.xuelang.pipeline.infrastructure.project.repository;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xuelang.hiforce.base.tracing.TracingUtils;
import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.domain.project.ProjectType;
import com.xuelang.pipeline.domain.project.repository.ProjectTypeRepository;
import com.xuelang.pipeline.infrastructure.project.converter.ProjectTypeToDOConverter;
import com.xuelang.pipeline.infrastructure.project.dataobject.ProjectTypeDO;
import com.xuelang.pipeline.infrastructure.project.mapper.ProjectTypeMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>项目类型仓库实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-09-02 17:17
 **/
@Service
public class ProjectTypeRepositoryImpl implements ProjectTypeRepository {

    @Autowired
    private ProjectTypeMapper mapper;

    @Override
    public Integer createProjectType(ProjectType projectType) {
        // 将 ProjectType 实体转换为 ProjectTypeDO
        ProjectTypeDO projectTypeDO = ProjectTypeToDOConverter.INSTANCE.toDO(projectType);
        int rows = mapper.insert(projectTypeDO);
        // 返回插入成功的项目类型ID
        return rows > 0 ? projectTypeDO.getId() : null;
    }

    @Override
    public boolean deleteProjectTypeById(Integer id) {
        // 根据项目类型ID删除项目类型
        return mapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateProjectType(ProjectType projectType) {
        // 将 ProjectType 实体转换为 ProjectTypeDO
        ProjectTypeDO projectTypeDO = ProjectTypeToDOConverter.INSTANCE.toDO(projectType);
        // 根据ID更新项目类型信息
        return mapper.updateById(projectTypeDO) > 0;
    }

    @Override
    public ProjectType findProjectTypeById(Integer id) {
        // 根据ID查询项目类型
        ProjectTypeDO projectTypeDO = mapper.selectById(id);
        // 将 ProjectTypeDO 转换为 ProjectType 实体
        return ProjectTypeToDOConverter.INSTANCE.fromDO(projectTypeDO);
    }

    @Override
    public List<ProjectType> findAllProjectTypes() {
        // 查询所有项目类型
        List<ProjectTypeDO> projectTypeDOList = mapper.selectList(null);
        if (CollectionUtils.isNotEmpty(projectTypeDOList)) {
            // 将 ProjectTypeDO 列表转换为 ProjectType 实体列表
            return projectTypeDOList.stream()
                    .map(ProjectTypeToDOConverter.INSTANCE::fromDO)
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public boolean batchUpdateProjectTypes(List<ProjectType> projectTypeList) {
        // 检查项目类型名称是否重复
        Set<String> typeNameSet = new HashSet<>();
        for (ProjectType projectType : projectTypeList) {
            if (!typeNameSet.add(projectType.getName())) {
                throw new BizException("项目类型名称重复: " + projectType.getName());
            }
        }

        // 拆分为需要更新和新增的列表
        List<ProjectTypeDO> updateList = Lists.newArrayList();
        List<ProjectTypeDO> insertList = Lists.newArrayList();
        for (ProjectType projectType : projectTypeList) {
            ProjectTypeDO projectTypeDO = ProjectTypeToDOConverter.INSTANCE.toDO(projectType);
            if (projectType.getId() != null) {
                updateList.add(projectTypeDO);
            } else {
                insertList.add(projectTypeDO);
            }
        }

        // 批量更新已有数据
        if (!updateList.isEmpty()) {
            mapper.batchUpdate(updateList, TracingUtils.getUserId(), new Date());
        }

        // 批量插入新的数据
        if (!insertList.isEmpty()) {
            mapper.batchInsert(insertList, TracingUtils.getUserId(), new Date());
        }

        return true;
    }
}
